﻿(function () {
    'use strict';
    var editor = null;
    // Custom event raised after the fragment is appended to the DOM.
    WinJS.Application.addEventListener('fragmentappended', function handler(e) {
        if (e.location === '/html/splitPage.html') {
            WinJS.xhr({ "url": e.state.ref }).then(function (response) {
                pageData.items = eval(response.responseText);
                var code = "";
                for (var i = 0; i < pageData.items.length; i++) {
                    code = pageData.items[i].source.toString();
                    pageData.items[i].source = code.replace(/function\s\(\)\s{/, "").replace(/}$/, "").replace(/\n\s\s\s\s\s\s\s\s/g, "\n");
                }
                fragmentLoad(e.fragment, e.state);
                document.getElementById('clear').addEventListener('click', console.clear, false);
                document.getElementById('submit').addEventListener('click', splitPage.exec, false);
            });
        }
    });

    function updateForLayout(lv, layout) {
        lv.layout = new WinJS.UI.ListLayout();
        lv.refresh();
    }

    function layoutChanged(e) {
        var list = document.querySelector('.itemList');
        if (list) {
            var lv = WinJS.UI.getControl(list);
            updateForLayout(lv, e.layout);
        }
    }

    function fragmentLoad(elements, options) {
        if (!WinJS.Navigation.canGoBack) {
            WinJS.Navigation.history.backStack[0] = { location: '/html/categoryPage.html' };
            var backButton = document.querySelector('header[role=banner] .win-backbutton');
            if (backButton) {
                backButton.removeAttribute('disabled');
            }
        }

        try {
            var appLayout = Windows.UI.ViewManagement.ApplicationLayout.getForCurrentView();
            if (appLayout) {
                appLayout.addEventListener('layoutchanged', layoutChanged);
            }
        } catch (e) { }

        splitPage.items = pageData.items
        splitPage.selectedItem = splitPage.items[0];

        elements.querySelector('.pageTitle').textContent = options.name;

        WinJS.UI.processAll(elements)
            .then(function () {
            var lv = WinJS.UI.getControl(elements.querySelector('.itemList'));
            lv.itemRenderer = elements.querySelector('.itemTemplate');
            updateForLayout(lv, Windows.UI.ViewManagement.ApplicationLayout.value);

            var details = elements.querySelector('.articleSection');
            return WinJS.Binding.processAll(details, splitPage.selectedItem);
        });


    }

    function itemInvoked(e) {
        var details = document.querySelector('.articleSection');
        splitPage.selectedItem = splitPage.items[e.detail.itemIndex];
        WinJS.Binding.processAll(details, splitPage.selectedItem);
        details.scrollTop = 0;
    }

    var pageData = {};

    WinJS.Namespace.define('splitPage', {
        fragmentLoad: fragmentLoad,
        itemInvoked: itemInvoked,
        exec: function () {
            try {
                eval(document.getElementById("code").innerText, window);
            } catch (e) {
                console.error(e.description);
            }
        },
        clear: console.clear
    });
})();